﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Kevin.Shop.Business.Core.Components.Cookie;
using Kevin.Shop.Business.Core.Member;
using Kevin.Shop.Business.Service.Entity.Helper;
using RiderShop.Mobile.Common.Component;
using RiderShop.Mobile.Common.Component.OAuth.Weixing;
using RiderShop.Mobile.Modle;
using Kevin.Shop.Business.Core.Components.Responser;

namespace RiderShop.WebSite.Controllers
{
    public partial class CustomerController : ThemeController
    {
        #region 登录
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="returnUrl"></param>
        /// <returns></returns>
        public ActionResult Login(string returnUrl)
        {
            if (WebClientContext.Current.IsAuthenticated)
            {
                if (String.IsNullOrEmpty(returnUrl))
                {
                    return RedirectToAction("Index", "Home");
                }
                {
                    return Redirect(returnUrl);
                }
            }
            return View(WebClientContext.Current.SiteTheme, "Customer", "Login", WebClientContext.Current.CacheKeyDefaultMasterPage, new CustomerModel());
        }

        /// <summary>
        /// 登陆
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="passWord"></param>
        /// <param name="returnUrl"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Login(string userName, string passWord, string returnUrl)
        {
            var model = new CustomerModel();
            if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(passWord))
            {
                model.Result = new Kevin.Shop.Business.Core.Components.Responser.ServiceResponser()
                {
                    Status = false,
                    ErrorMessage = "账号名和密码不能为空"
                };
                return View(WebClientContext.Current.SiteTheme, "Customer", "Login", WebClientContext.Current.CacheKeyDefaultMasterPage, model);
            }
            model.Login(userName, passWord);
            if (model.Result == null || !model.Result.Status)
            {
                model.ReturnUrl = returnUrl;
                return View(WebClientContext.Current.SiteTheme, "Customer", "Login", WebClientContext.Current.CacheKeyDefaultMasterPage, model);
            }
            HttpCookie authCookie = FormsAuthentication.GetAuthCookie(userName, true);
            KevinCookieManager.Set(authCookie.Name, authCookie.Value, 10000);
            WebClientContext.Current.GetCurrentUser();
            FormsAuthentication.RedirectFromLoginPage(userName, true);
            if (!String.IsNullOrEmpty(returnUrl))
            {
                return Redirect(returnUrl);
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }
        }
        #endregion

        #region 注册
        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public ActionResult Register(UserBasicInfo user)
        {
            string returnUrl = Request.Form["returnUrl"];
            var model = new CustomerModel();
            if (string.IsNullOrEmpty(user.UserName))
            {
                //简化注册流程。省略用户名
                user.UserName = user.Email;
                user.RealName = user.CellPhone;
            }
            if (user == null || string.IsNullOrEmpty(user.UserName))
            {
                return View(WebClientContext.Current.SiteTheme, "Customer", "Register", WebClientContext.Current.CacheKeyDefaultMasterPage, model);
            }
            model.Register(user);
            if (!model.Result.Status)
            {
                model.ReturnUrl = returnUrl;
                return View(WebClientContext.Current.SiteTheme, "Customer", "Register", WebClientContext.Current.CacheKeyDefaultMasterPage, model);
            }
            HttpCookie authCookie = FormsAuthentication.GetAuthCookie(user.UserName, true);
            KevinCookieManager.Set(authCookie.Name, authCookie.Value, 10000);
            WebClientContext.Current.GetCurrentUser();
            if (!String.IsNullOrEmpty(returnUrl))
            {
                return Redirect(returnUrl);
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }
        }

        /// <summary>
        /// 检查验证码是否正确
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpGet]
        public JsonResult CheckVerifyCode(string code)
        {
            bool isVerifyCode = true;
            if (!FindPasswordModel.CheckVerifyCode(code))
            {
                isVerifyCode = false;
            }

            return Json(isVerifyCode, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 登出
        /// <summary>
        /// 登出
        /// </summary>
        /// <returns></returns>
        public ActionResult Logout()
        {
            FormsAuthentication.SignOut();
            KevinCookieManager.Remove("openid");
            return RedirectToAction("Index", "Home");
        }
        #endregion
    }
}
